package com.jianbing.util;

import java.io.*;

/**
 * Created by Han,Tixiang on 2016/8/16 0016.
 */
public class ObjectUtils {


    public static Object byteToObject(byte[] bytes) {
        Object obj = null;
        try {
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);
            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
        }
        return obj;
    }

    public static void serialize(Object object, String filePath , String fileName) throws IOException {
        File file = new File(filePath + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.close();
    }

    public static Object deSerialize(String fileName) {
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                return null;
            }
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                    file));
            return ois.readObject();
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
       /* String date = DateUtils.yyyymmddFormat(new Date());
        LogCount logCount = new LogCount("1");
        logCount.getWebsocket_subscribe_success().incrementAndGet();


        ObjectUtils.serialize(logCount, CommonConstant.objectTmpPath, CommonConstant.report + "." + date);


        Object obj = ObjectUtils.deSerialize(CommonConstant.objectTmpPath + CommonConstant.report + "." + date);
        if (obj != null && obj instanceof LogCount) {
            LogCount log = (LogCount) obj;
            System.out.println(log.getWebsocket_subscribe_success().get());
        }*/
    }
}
